package com.yq.core.util;

import com.yq.api.dto.ProductTO;
import com.yq.core.dto.ProductDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yq on 16-8-7.
 */
public class ProductDOUtil {
    public static ProductDO toProductDO(ProductTO productTO){
        if(productTO == null){
            return null;
        }
        ProductDO productDO = new ProductDO();
        BeanUtils.copyProperties(productTO, productDO);
        return productDO;
    }

    public static void writeBack(ProductDO productDO, ProductTO productTO){
        if(productDO == null || productTO == null){
            return;
        }
        BeanUtils.copyProperties(productDO, productTO);
    }

    public static ProductTO toProductTO(ProductDO productDO) {
        if (productDO == null) {
            return null;
        }
        ProductTO productTO = new ProductTO();
        BeanUtils.copyProperties(productDO, productTO);
        return productTO;
    }

    public static List<ProductTO> toProductTOList(List<ProductDO> productDOList) {
        if (CollectionUtils.isEmpty(productDOList)) {
            return null;
        }
        List<ProductTO> productTOList = new ArrayList<ProductTO>();
        for (ProductDO productDO : productDOList) {
            ProductTO productTO = toProductTO(productDO);
            productTOList.add(productTO);
        }
        return productTOList;
    }
}
